using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using aoi;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            AoiScene scene1 = AoiManager.CreateScene(1, new Rectangle(0, 0, 14, 10));
            AoiScene scene2 = AoiManager.CreateScene(2, new Rectangle(10, 0, 14, 10));
            AoiScene scene3 = AoiManager.CreateScene(3, new Rectangle(20, 0, 14, 10));

            AoiManager.dump_cells(scene1);
            AoiManager.dump_cells(scene2);
            AoiManager.dump_cells(scene3);

            AoiManager.appearHandler += (obj, list) =>
            {
                Console.Write("appear: {0} <= ", obj.id);
                foreach (var o in list)
                {
                    Console.Write("{0} ", o.id);
                    obj.relate_list.Add(o);
                    o.relate_list.Add(obj);
                }
                Console.WriteLine();
            };
            AoiManager.disappearHandler += (obj, list) =>
            {
                Console.Write("disappear: {0} <= ", obj.id);
                foreach (var o in list)
                {
                    Console.Write("{0} ", o.id);
                    obj.relate_list.Remove(o);
                    o.relate_list.Remove(obj);
                }
                Console.WriteLine();
            };
            AoiManager.sceneChangedHandler += (obj, s1, s2) =>
            {
                Console.WriteLine("changeScene: {0}, {1}->{2}", obj.id, s1.id, s2.id);
            };

            Console.WriteLine("--------- test simple add/remove ----------");
            var o1 = new AoiObject() { id = 1, x = 11, y = 5 };
            var o2 = new AoiObject() { id = 2, x = 10, y = 5 };
            var o3 = new AoiObject() { id = 3, x = 10, y = 6 };
            AoiManager.AddObject(scene1, o1);
            AoiManager.AddObject(scene1, o2);
            AoiManager.AddObject(scene1, o3);
            dump(o1, o2, o3);
            AoiManager.RemoveObject(o2);
            AoiManager.RemoveObject(o1);
            dump(o1, o2, o3);
            Console.WriteLine("--------- test move ----------");
            AoiManager.AddObject(scene1, o2);
            dump(o1, o2, o3);
            for (int x = o2.x+1; x <= 16; x++)
            {
                AoiManager.MoveObject(o2, x, o2.y);
            }
            for (int x = o2.x - 1; x >= 6; x--)
            {
                AoiManager.MoveObject(o2, x, o2.y);
            }
            dump(o1, o2, o3);
        }

        static void dump(params AoiObject[] objs)
        {
            Console.WriteLine("dump: ");
            foreach (var obj in objs)
            {
                if (obj.cell == null) continue;
                Console.Write("\t{0}: ", obj.id);
                foreach (var o in obj.relate_list)
                {
                    Console.Write("{0} ", o.id);
                }
                Console.WriteLine();
            }
        }
    }
}
